package com.sisyphus.mock.controllers;

import com.sisyphus.mock.data.AppDatabase;
import com.sisyphus.mock.data.PostMapper;
import com.sisyphus.mock.data.UserMapper;
import com.sisyphus.mock.data.UserPostFavoriteMapper;
import com.sisyphus.mock.data.entities.PostEntity;
import com.sisyphus.mock.data.entities.UserPostFavoriteEntity;
import com.sisyphus.mock.data.model.BaseResponse;
import com.sisyphus.mock.data.entities.UserEntity;
import com.sisyphus.mock.utils.EncryptUtil;
import com.sisyphus.mock.utils.TokenUtil;
import com.yanzhenjie.andserver.annotation.PostMapping;
import com.yanzhenjie.andserver.annotation.RequestHeader;
import com.yanzhenjie.andserver.annotation.RequestMapping;
import com.yanzhenjie.andserver.annotation.RequestParam;
import com.yanzhenjie.andserver.annotation.RestController;

@RestController
@RequestMapping(path = "/user")
public class UserController {

    private final UserMapper mUserMapper = AppDatabase.get().userMapper();

    private final PostMapper mPostMapper = AppDatabase.get().postMapper();

    private final UserPostFavoriteMapper mUserPostFavoriteMapper
            = AppDatabase.get().userPostFavoriteMapper();


    /**
     * 用户注册
     */
    @PostMapping("/register")
    public BaseResponse<Object> register(
            @RequestParam("email") String email,
            @RequestParam("nickname") String nickname,
            @RequestParam("password") String password
    ) {

        if (email == null || email.trim().isEmpty()) {
            return BaseResponse.fail("邮箱错误");
        }

        if (nickname == null || nickname.trim().isEmpty()) {
            return BaseResponse.fail("用户名错误");
        }

        if (password == null || password.trim().isEmpty()) {
            return BaseResponse.fail("密码错误");
        }

        UserEntity user = mUserMapper.findByEmail(email);
        if (user != null) {
            return BaseResponse.fail("邮箱已注册");
        }

        // 写入
        user = new UserEntity(email, null, EncryptUtil.md5(password), nickname, null);
        mUserMapper.insertUser(user);
        return BaseResponse.success();
    }


    @PostMapping("/login")
    public BaseResponse<UserEntity> login(
            @RequestParam("email") String email,
            @RequestParam("password") String password
    ) {

        UserEntity user = mUserMapper.findByEmail(email);
        if (user == null) {
            return BaseResponse.fail("邮箱未注册");
        }

        user = mUserMapper.findByEmail(email, EncryptUtil.md5(password));
        if (user == null) {
            return BaseResponse.fail("密码错误");
        }

        // 生成并写入token
        user.token = TokenUtil.generateToken(user);
        mUserMapper.updateUser(user);
        user.password = null; // 不显示密码

        return BaseResponse.success(user);
    }


    /**
     * 用户信息
     */
    @PostMapping("/info")
    public BaseResponse<UserEntity> info(@RequestHeader("token") String token) {

        if (token == null) {
            return BaseResponse.fail(BaseResponse.UNAUTHORIZED_CODE, "用户未登录");
        }

        return BaseResponse.success(mUserMapper.findByToken(token));
    }

    /**
     * 收藏指定帖子
     */
    @PostMapping("/post/favorite")
    BaseResponse<Object> postFavorite(
            @RequestHeader("token") String token,
            @RequestParam("post_id") long postId
    ) {

        UserEntity userEntity = mUserMapper.findByToken(token);
        if (userEntity == null) {
            return BaseResponse.fail(BaseResponse.UNAUTHORIZED_CODE, "token失效，请重新登录");
        }

        PostEntity postEntity = mPostMapper.findById(postId);
        if (postEntity == null) {
            return BaseResponse.fail("没有该帖子");
        }

        // 是否已经收藏
        UserPostFavoriteEntity byPostId = mUserPostFavoriteMapper.findByPostId(postId);
        if (byPostId != null) {
            return BaseResponse.fail("已经收藏该帖子");
        }

        mUserPostFavoriteMapper.insert(new UserPostFavoriteEntity(userEntity.id, postId, System.currentTimeMillis()));

        return BaseResponse.success();
    }

}
